<?php

namespace backend\modules\metting\controllers;

use common\models\metting\Metting;
use Yii;
use common\enums\StatusEnum;
use common\traits\Curd;
use common\models\base\SearchModel;
use common\models\backend\Notify;
use backend\controllers\BaseController;

/**
 * 知识分享会
 * Class NotifyAnnounceController
 * @package backend\modules\base\controllers
 * @author jianyan74 <751393839@qq.com>
 */
class MettingController extends BaseController
{
    use Curd;

    /**
     * @var Notify
     */
    public $modelClass = Metting::class;

    /**
     * @return string
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionIndex()
    {
        $searchModel = new SearchModel([
            'model' => $this->modelClass,
            'scenario' => 'default',
            'partialMatchAttributes' => ['author','title'], // 模糊查询
            'defaultOrder' => [
                //'id' => ID_DESC,
            ],
            'pageSize' => $this->pageSize,
        ]);
        $dataProvider = $searchModel
            ->search(Yii::$app->request->queryParams);
        $dataProvider->query
            ->andWhere(['>=', 'status', StatusEnum::DISABLED]);
        return $this->render($this->action->id, [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
            'type'=>['1'=>'已结束','2'=>'预约'],
        ]);
    }

    /**
     * 编辑/创建
     *
     * @return mixed
     */
    public function actionEdit()
    {
        $id = Yii::$app->request->get('id', null);
        $model = $this->findModel($id);
        $data = Yii::$app->request->post();
        //var_dump(date('Y-m-d',time()));
//       dd($data);die();
        if($model->load($data)){
            if($data['Metting']['date']>date('Y-m-d',time())){
                $data['Metting']['type']=2;
            }else{
                $data['Metting']['type']=1;
            }
       }
        if ($model->load($data) && $model->save()) {
            return $this->redirect(['index']);
        }
        return $this->render($this->action->id, [
            'model' => $model,
            'type'=>['1'=>'已结束','2'=>'预约'],
        ]);
    }
}